using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controls.Support;
using Nullables;

namespace Sedna.Core.Controls.Support
{
	[ValidationProperty("Text")]
	public abstract class BaseEnter : BaseControl, INamingContainer, IClientScriptingControl
	{	 
		protected TextBox textBox = new TextBox("textBox");
        private BasePopupButton popupButton;	    	    
	    
	    public new IEnterConfiguration Configuration
	    {
            get { return (IEnterConfiguration) base.Configuration; }
            set { base.Configuration = value; }
	    }
	    
		public BasePopupButton PopupButton
		{
            get { return popupButton; }
		} 

		public virtual string Text 
		{
			get { return textBox.Text; }
			set { textBox.Text = value; }
		}


		public IDictionary ParameterControls
		{
			get { return PopupButton.ParameterControls;}
		}

		public IDictionary DisplayControls
		{
			get {return PopupButton.DisplayControls;}
		}

		protected override HtmlTextWriterTag TagKey
		{
			get { return HtmlTextWriterTag.Table; }
		}
	    
	    public bool AllowManualEnter
	    {
            get { return textBox.Enabled; }
	        set
	        {
                textBox.Enabled = value && Enabled; 
	        }
	    }

		public override bool Enabled
		{
			get { return base.Enabled; }
			set
			{
                EnsureChildControls();
				base.Enabled = value;
			    
				PopupButton.Enabled = value;
			    
			    if (!value)
			        AllowManualEnter = false;
			}
		}
	    

		public BaseEnter(IConfiguration configuration) : base(configuration)
		{
			Width=Unit.Pixel(120);
			textBox.EnableViewState=true;
		}

		protected override void Build()
		{
			base.Build();
		    
			Enabled = configuration.IsEnabledForPrincipal() && configuration.Enabled;
			Visible = configuration.IsVisibleForPrincipal();

			IEnterConfiguration enterConfiguration = configuration as IEnterConfiguration;
			if (enterConfiguration == null)
			{
				return;
			}

            AllowManualEnter = enterConfiguration.AllowManualEnter;
			PopupButton.ParameterControls = enterConfiguration.ParameterControls;
			PopupButton.DisplayControls = enterConfiguration.DisplayControls;
			PopupButton.ShowUnnamed = enterConfiguration.ShowUnnamed;
			PopupButton.ShowInactive = enterConfiguration.ShowInactive;
			textBox.MaxLength = enterConfiguration.MaxLength;
            ControlUtil.SetupClientAttributes(textBox, enterConfiguration);
			PopupButton.HookupParameterControlsEvents = enterConfiguration.HookupParameterControlsEvents;
		}

		protected override void OnLoad(EventArgs e)
		{
			EnsureChildControls();		   		    
		    
			base.OnLoad(e);

            if (Configuration.VisibleBindPath != null)
                this.Visible = true; // To reach the OnPreRender method and analyze visibility in the Bind method
		}

		protected override void CreateChildControls() 
		{
            popupButton = BuildPopupButton();


            Panel wrapper = new Panel();
            Controls.Add(wrapper);


		    wrapper.CssClass = "baseEnter";
		    textBox.CssClass = "baseEnter";
            
            popupButton.CssClass = "baseEnter";
            PopupButton.Text = "...";
            PopupButton.ID = "buttonEnter";

            wrapper.Controls.Add(textBox);
            wrapper.Controls.Add(popupButton);
            

            //TableRow row = new TableRow();
           //Controls.Add(row);

            //TableCell textBoxCell=new TableCell();
            //textBoxCell.Width=Unit.Percentage(80);
            //row.Controls.Add(textBoxCell);

            //TableCell spacerCell=new TableCell();
            //spacerCell.Width=Unit.Percentage(0);
            //spacerCell.Controls.Add(new System.Web.UI.LiteralControl("&nbsp;"));
            //row.Controls.Add(spacerCell);

            //TableCell buttonCell=new TableCell();
            //buttonCell.Width=Unit.Percentage(20);
            //row.Controls.Add(buttonCell);


			//textBox.Width=Unit.Percentage(100);
		   // textBox.Style.Add("width","100%");

			//Extra table is needed to workaround IE bug:
			//if <INPUT> has too long value <TD> expands to its width
//            textBoxCell.Controls.Add(new System.Web.UI.LiteralControl("<table cellpadding=\"0\" cellspacing=\"0\" style=\"width:100%;table-layout:fixed;border-collapse:collapse\" ><tr><td>"));
//            textBoxCell.Controls.Add(textBox);
//            textBoxCell.Controls.Add(new System.Web.UI.LiteralControl("</td></tr></table>"));

//            buttonCell.Controls.Add(PopupButton);

		}

        protected abstract BasePopupButton BuildPopupButton();
	    
		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			base.AddAttributesToRender(writer);
			writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
			writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
			writer.AddAttribute(HtmlTextWriterAttribute.Align, "left");
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			//Needed for successful parameter controls resolving.
			PopupButton.ParameterControlsNamingContainer=NamingContainer;

			PopupButton.ResolveDisplayControls=false;
			PopupButton.DisplayControls=ControlUtil.ResolveClientScriptingControls(DisplayControls, NamingContainer);
		    
		    if (Visible)
		    {
		        EnableClientScripting();
		    }
		}

		protected virtual void EnableClientScripting()
		{
			PopupButton.ClientScriptingEnabled=true;
			textBox.ClientScriptingEnabled=true;
		}

		protected override void Unbind()
		{
			binder.Unbind("Enabled");
            binder.Unbind("Visible");
            binder.Unbind("AllowManualEnter");		    
		}

		protected override void Bind()
		{
            this.Visible = this.configuration.Visible;
		    
			binder.Bind("Enabled");
            binder.Bind("Visible");
            binder.Bind("AllowManualEnter");

            this.Enabled = this.Enabled && this.configuration.IsEnabledForPrincipal();
            this.Visible = this.Visible && this.configuration.IsVisibleForPrincipal();
		}

	}
}
 